package com.itheima.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController//包含(@Controller  @ResponseBody)
//@Controller
@RequestMapping("json")
public class JsonController {
    //1.传统方式向页面输出字符串
    @RequestMapping("test1")
    public void test1(HttpServletResponse response) throws IOException {
        //向页面输出ok
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("吕布");
    }

    //2.springmvc
    @RequestMapping(value = "test2",produces = "text/html;charset=utf-8")
    @ResponseBody//阻止视图跳转,向页面输出字符串
    public String test2() {
        return "貂蝉";
    }

    @RequestMapping(value = "test3")

    @ResponseBody
    /*作用
     * 1.阻止视图跳转,向页面输出字符串
     * 2.将对象转换成json格式字符串
     *
     *
     * @RequestBody:将json格式字符串转换为对应的对象类型
     * */
    public String test3() {
        /*User u= new User();*/


        return "貂蝉";
    }
}
